package com.qfedu.meidijiaju.service.impl;

import com.qfedu.meidijiaju.common.ResponseData;
import com.qfedu.meidijiaju.entity.FaxiantieziYxy;
import com.qfedu.meidijiaju.entity.PinglunYxy;
import com.qfedu.meidijiaju.mapper.FaxiantieziYxyMapper;
import com.qfedu.meidijiaju.mapper.HuatiYxyMapper;
import com.qfedu.meidijiaju.mapper.PinglunYxyMapper;
import com.qfedu.meidijiaju.service.FaxiantieziYxyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 发现帖子 服务实现类
 * </p>
 *
 * @author meidimeiju
 * @since 2022-10-03
 */
@Service
public class FaxiantieziYxyServiceImpl extends ServiceImpl<FaxiantieziYxyMapper, FaxiantieziYxy> implements FaxiantieziYxyService {

    @Autowired
    private FaxiantieziYxyMapper faxiantieziYxyMapper;

    @Override
    public ResponseData<List<FaxiantieziYxy>> allFaXianTieZiByFaXianId(String faxiantypeId) {

        List<FaxiantieziYxy> faxiantieziYxyList = faxiantieziYxyMapper.allFaXianTieZiByFaXianId(faxiantypeId);

        ResponseData<List<FaxiantieziYxy>> responseData = new ResponseData<>();

        if (faxiantieziYxyList != null && faxiantieziYxyList.size() > 0){

            responseData.setCode(200);

            responseData.setData(faxiantieziYxyList);

        } else {

            responseData.setCode(400);

            responseData.setMsg("无数据！");
        }

        return responseData;
    }

    @Autowired
    private PinglunYxyMapper pinglunYxyMapper;

    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,timeout = 10,readOnly = false)
    @Override
    public ResponseData<FaxiantieziYxy> findFaXianTieZiByTieZiId(int tieziId) {

        List<PinglunYxy> pinglunYxyList = pinglunYxyMapper.findPinglun(tieziId);

        int result = faxiantieziYxyMapper.updateFaXianTieZiByPingLunNum(pinglunYxyList.size(),tieziId);

        ResponseData<FaxiantieziYxy> responseData = new ResponseData<>();

        FaxiantieziYxy faxiantieziYxy = faxiantieziYxyMapper.findFaXianTieZiByTieZiId(tieziId);

        if (faxiantieziYxy != null){

            responseData.setCode(200);

            responseData.setData(faxiantieziYxy);
        } else {

            responseData.setCode(200);

            responseData.setMsg("无数据");
        }
        return responseData;
    }

    @Override
    public ResponseData<String> addFaXianTieZi(FaxiantieziYxy faxiantieziYxy) {

        ResponseData<String> responseData = new ResponseData<>();

        if (faxiantieziYxy != null && !StringUtils.isEmpty(faxiantieziYxy.getTizibiaoti())
                                   && !StringUtils.isEmpty(faxiantieziYxy.getTiezineirong())
        ){
            int result = faxiantieziYxyMapper.addFaXianTieZi(faxiantieziYxy);

            if (result == 1){

                responseData.setCode(200);

                responseData.setMsg("添加成功！");

            } else {

                responseData.setCode(500);

                responseData.setMsg("添加失败！");
            }
        } else {

            responseData.setCode(400);

            responseData.setMsg("请输入信息！");

        }
        return responseData;
    }

    @Override
    public ResponseData<List<FaxiantieziYxy>> findFaXianTieZiByLike(String contentOrTitle) {

        ResponseData<List<FaxiantieziYxy>> responseData = new ResponseData<>();

        if (!StringUtils.isEmpty(contentOrTitle)){

            List<FaxiantieziYxy> faxiantieziYxyList = faxiantieziYxyMapper.findFaXianTieZiByLike("%"+contentOrTitle+"%");

            if (faxiantieziYxyList != null && faxiantieziYxyList.size() > 0){

                responseData.setCode(200);

                responseData.setData(faxiantieziYxyList);

            } else {

                responseData.setCode(400);

                responseData.setMsg("无数据！");

            }
        } else {

            responseData.setCode(400);

            responseData.setMsg("请输入要查询的内容！");
        }
        return responseData;
    }

    @Autowired
    private HuatiYxyMapper huatiYxyMapper;

    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,timeout = 10,readOnly = false)
    @Override
    public ResponseData<List<FaxiantieziYxy>> findFaXianTieZiByHuaTiId(int huaTiId) {

        ResponseData<List<FaxiantieziYxy>> responseData = new ResponseData<>();

        List<FaxiantieziYxy> faxiantieziYxyList = faxiantieziYxyMapper.findFaXianTieZiByHuaTiId(huaTiId);

        int result = huatiYxyMapper.updateHuaTiTaoLunNum(faxiantieziYxyList.size(),huaTiId);

        List<FaxiantieziYxy> faxiantieziYxies = faxiantieziYxyMapper.findFaXianTieZiByHuaTiId(huaTiId);


        if (faxiantieziYxies != null && faxiantieziYxies.size() > 0){

            responseData.setCode(200);

            responseData.setData(faxiantieziYxies);
        } else {

            responseData.setCode(400);

            responseData.setMsg("该话题暂时无帖子，您可以成为第一个！！！");
        }

        return responseData;
    }
}
